﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace RG.Common.WPF
{
    public interface IToolbarButton
    {
        string Header { get; set; }
        string ToolTipText { get; set; }
        string SecondHeader { get; set; }
        string ImagePath { get; set; }
        ICommand Command { get; set; }
        string VisibleFlag { get; set; }
        bool IsMenuItem { get; set; }
        object CommandParameter { get; set; }
    }

    [SupportDataBinding]
    public class ToolbarButton : Control, IToolbarButton
    {
        public string Header{ get; set; }

        public string SecondHeader { get; set; }

        public string ToolTipText{ get; set; }

        public string ImagePath{ get; set; }

        public ICommand Command { get; set; }

        public string VisibleFlag { get; set; }
        
        public bool IsMenuItem { get; set; }
        
        public object CommandParameter{ get; set;}

    }

    public class ToolbarSeparator : IToolbarButton
    {
        public string Header { get; set; }

        public string ToolTipText { get; set; }

        public string SecondHeader { get; set; }

        public string ImagePath { get; set; }

        public ICommand Command { get; set; }

        public string VisibleFlag { get; set; }
        
        public bool IsMenuItem { get; set; }

        public object CommandParameter { get; set; } 
    }



    [ValueConversion(typeof(IEnumerable<IToolbarButton>), typeof(IEnumerable<FrameworkElement>))]
    public class ToolBarConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ret = new List<FrameworkElement>();

            foreach (var item in (IEnumerable<IToolbarButton>)value)
            {
                if (item is ToolbarButton)
                {
                    var data = (ToolbarButton)item;
                    if (data.IsMenuItem)
                    {
                        var mi = new MenuItem
                        {
                            Header = data.Header,
                            Margin = new Thickness(5, 0, 5, 0),
                            Command = data.Command,
                            ToolTip = data.ToolTipText,
                            CommandParameter = data.CommandParameter
                        };
                        ret.Add(mi);
                    }
                    else
                    {
                        var button = new Button
                                         {
                                             Content = CreateButtonTemplate(data),
                                             VerticalAlignment = VerticalAlignment.Top,
                                             Margin = new Thickness(5, 0, 5, 0),
                                             Command = data.Command,
                                             ToolTip = data.ToolTipText,
                                             CommandParameter = data.CommandParameter
                                         };
                        ret.Add(button);
                        ToolBar.SetOverflowMode(button, OverflowMode.Never);
                    }
                }
                else if (item is ToolbarSeparator) ret.Add(new Separator());
            }
            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        private static ContentControl CreateButtonTemplate(IToolbarButton sr)
        {
            var ret = new ContentControl();
            var stack = new StackPanel{VerticalAlignment = VerticalAlignment.Top};

            if(!string.IsNullOrEmpty(sr.ImagePath))
            {
                var icon = new Image { Source = new BitmapImage(new Uri(sr.ImagePath)), Width = 28.0 };
                stack.Children.Add(icon);
            }

            var label = new TextBlock { Text = sr.Header, HorizontalAlignment = HorizontalAlignment.Center};
            if (!string.IsNullOrEmpty(sr.VisibleFlag))
            {
                var binding = new Binding { Path = new PropertyPath(sr.VisibleFlag), Converter = new VisibilityConverter() };
                label.SetBinding(UIElement.VisibilityProperty, binding);
            }

            stack.Children.Add(label);
            if (!string.IsNullOrEmpty(sr.SecondHeader))
            {
                var label2 = new TextBlock { Text = sr.SecondHeader, HorizontalAlignment = HorizontalAlignment.Center };
                if (!string.IsNullOrEmpty(sr.VisibleFlag))
                {
                    var binding = new Binding { Path = new PropertyPath(sr.VisibleFlag), Converter = new VisibilityConverter() };
                    label2.SetBinding(UIElement.VisibilityProperty, binding);
                }

                stack.Children.Add(label2);
            }
            ret.Content = stack;
            return ret;
        }
    }

    //здесь ибо это глобальные константы для всех проектов использующие Common.WPF
    public static class ToolBarIcon
    {
        
        public static string AddIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/add.png";
        public static string ExcelIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/excel.png";
        public static string CalcIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/calc.png";
        public static string ChangeMarkIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/changemark.png";
        public static string CheckMarkIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/checkmark.png";
        public static string CheckIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/check.png";
        public static string CloseDocIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/closeDoc.png";
        public static string CopyIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/copy.png";
        public static string CutIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/cut.png";
        public static string DeleteIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/delete.png";
        public static string DeleteRoundIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/deleteSmall.png";
        public static string DismissIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/dismiss.png";
        public static string EditIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/fileEdit.png";
        public static string FolderIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/folder.png";
        public static string FileDownIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/filedown.png";
        public static string NewDocIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/fileNew.png";
        public static string GotoDocIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/gotoDoc.png";
        public static string HistoryIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/history.png";
        public static string MailIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/mail.png";
        public static string MoveIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/move.ico";
        public static string OpenDocIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/open.png";
        public static string PasteIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/paste.png";
        public static string PrintPreviewIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/printPreview.png";
        public static string RefreshIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/refresh.png";
        public static string RefusalIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/printPreview.png";
        public static string ReceiptIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/checknew.png";
        public static string SaveIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/save.png";
        public static string SaveAllIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/saveAll.png";
        public static string ShowFileIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/showFile.png";
        public static string UserIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/user.png";
        public static string UserPlusIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/userPlus.png";
        public static string ZoomInIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/zoomIn.png";
        public static string ZoomOutIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/zoomOut.png";
        public static string ChangeIcon = "pack://application:,,,/RG.Common.WPF;component/ToolBarImages/arrowupdown.png";
        
    }
}
